<template>
	<z-paging ref="paging" v-model="listData" @query="queryList">
		<template #refresher="{refresherStatus}">
			<!-- 此处的custom-refresh为demo中自定义的组件，非z-paging的内置组件，请在实际项目中自行创建。这里插入什么view，下拉刷新就显示什么view -->
			<custom-refresher :status="refresherStatus" />
		</template>
		<image class="header-img" :src="imageUrl + loopImgCover" mode="aspectFill"></image>
		<view class="commit-card" v-for="(item,index) in listData" :key="item.CommunityID || index">
			<view class="user-box" @click="gotoPageSelf(item.CommunityID)">
				<view class="UserImage">
					<image class="Avatar" :src="imageUrl+item?.pub_img" mode="aspectFill"></image>
					<view class="Name">{{item.pub_nick_name}}</view>
				</view>
				<uni-icons type="more-filled" size="24"></uni-icons>
			</view>
			<view class="Content-title" @click="gotoPageSelf(item.CommunityID)">{{item.trip_blog_title}}</view>
			<text class="Content-text" @click="gotoPageSelf(item.CommunityID)">{{item.trip_blog_content}}</text>
			<view class="imageAll-box" @click="gotoPageSelf(item.CommunityID)"> 
				<block v-for="(itemImg,inde) in item.img_list" :key="index">
					<image class="item-iameg" mode="aspectFill" :src="imageUrl+itemImg"></image>
				</block>
			</view>
			<view class="icon-box">
				<text class="Views">{{ item.blog_view || '0' }}次浏览</text>
				<view class="icon-item" @click="isLiack(item.CommunityID,index)">
					<uni-icons v-if="item.is_like==1" color="#fc394b" type="hand-up-filled" size="18"></uni-icons>
					<uni-icons v-if="item.is_like==0" type="hand-up" size="18"></uni-icons>
					<view class="iconNumber">{{item.blog_like}}</view>
				</view>
	<!-- 			<view class="icon-item" @click="isCollect(item.CommunityID,index)">
					<uni-icons v-if="item.is_collect==1" color="#fc394b" type="heart-filled" size="18"></uni-icons>
					<uni-icons v-if="item.is_collect==0" type="heart" size="18"></uni-icons>
					<view class="iconNumber">{{item.blog_collect}}</view>
				</view> -->
			</view>
		</view>
	</z-paging>
</template>

<script setup>
	import {ref,watchEffect} from 'vue';
	import { imageUrl } from '@/config/request/request';
	import { PostLike,PostCollect } from '@/config/API/commit';
	import { PostcommunityInfo } from '@/config/API/commit';
	import {routerGuard} from '@/config/RouteGuard.js'
	const paging = ref(null); // 用于控制z-paging
	// const PageDataStrat = ref(1); // 起始页码
	// const PageSize = ref(5); // 每页条目数
	const listData = ref([])
	const propos = defineProps({
		loopImgCover: String,
		cardData: {
			type: Array,
			default: () => []
		},
		userId:{
			type:Number,
			default: -1
		},
		loopId:{
			type:Number,
			default: 0
		},
	})
	watchEffect(()=>{
		listData.value = propos.cardData
		console.log('父组件传过来的社区数据',propos);
	})
	
	// 分页查询函数
	const queryList = async (pageNo, pageSize) => {
		console.log('分页查询：', { pageNo, pageSize });
		const itemFrom = {
			page: pageNo,
			size: pageSize,
			user_id: propos.userId,
			loopID: propos.loopId
		};
	
		try {
			const resData = await PostcommunityInfo(itemFrom);
			// 如果请求成功，将数据传递给z-paging的complete方法
			if (resData && resData.data) {
				paging.value.complete(resData.data); // 数据加载完成
			} else {
				paging.value.complete(false); // 无数据
			}
		} catch (err) {
			console.error('分页请求失败：', err);
			paging.value.complete(false); // 出现错误时标记分页为结束
		}
	};
	const isLiack = async(id,index) =>{
		const data = await PostLike(id)
		console.log('是否点赞成功===>>',data)
		if(data.code==200){
			propos.cardData[index].is_like==0?propos.cardData[index].is_like=1:propos.cardData[index].is_like=0
			propos.cardData[index].is_like==1?propos.cardData[index].blog_like+=1:propos.cardData[index].blog_like-=1
		}else if(data.code==309){
			uni.clearStorageSync()
			uni.navigateTo({
				url: '/pages/Login/Login'
			});
		}
	}
	// 点击收藏暂时不要 
	// const isCollect = async(id,index)=>{
	// 	const data = await PostCollect(id)
	// 	console.log('是否收藏成功===>>',data)
	// 	if(data.code==200){
	// 		propos.cardData[index].is_collect==0?propos.cardData[index].is_collect=1:propos.cardData[index].is_collect=0
	// 		propos.cardData[index].is_collect==1?propos.cardData[index].blog_collect+=1:propos.cardData[index].blog_collect-=1
	// 	}else if(data.code==309){
	// 		uni.navigateTo({
	// 			url: '/pages/Login/Login'
	// 		});
	// 	}
	// }
	const gotoPageSelf = (id) => {
		const url = `/pages/community/commitDetails?id=${id}`
		routerGuard(url);
	}
</script>

<style scoped lang="scss">
.header-img{
	width: 100%;
	height: 350rpx;
	border-radius: 14rpx;
	margin: 30rpx 0 70rpx 0;
}
.commit-card{
	width: 100%;
	min-height: 300rpx;
	margin-bottom: 80rpx;
	.user-box{
		display: flex;
		align-items: center;
		justify-content: space-between;
		.UserImage{
			display: flex;
			align-items: center;
			gap: 20rpx;
			.Avatar{
				border-radius: 50%;
				width: 100rpx;
				height: 100rpx;
			}
			.Name{
				max-width: 65vw;
				font-size: 30rpx;
				color: #8f8f8f;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 1; /* 设置最大行数 */
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}
	.Content-title{
		margin-top: 30rpx;
		font-size: 30rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2; /* 设置最大行数 */
		overflow: hidden;
		text-overflow: ellipsis;
		line-height: 1.5em; /* 每行的高度 */
		word-break: break-all; /* 防止长单词导致布局破坏 */
	}
	.Content-text{
		color: #333333;
		font-size: 28rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 4; /* 设置最大行数 */
		overflow: hidden;
		text-overflow: ellipsis;
		line-height: 1.5em; /* 每行的高度 */
		word-break: break-all; /* 防止长单词导致布局破坏 */
	}

	.imageAll-box {
		margin: 30rpx 0;
		display: flex;
		gap: 15rpx;
		flex-wrap: wrap;
		.item-iameg {
			border-radius: 14rpx;
			width: calc((100% - 30rpx) / 3);
			height: 190rpx;
		}
	}
	.icon-box{
		margin-top: 10rpx;
		justify-content: flex-end;
		display: flex;
		gap: 80rpx;
		align-items: center;
		.Views{
			font-size: 26rpx;
			color: #8f8f8f;
			font-family: 300;
			letter-spacing: 3rpx;
		}
		.icon-item{
			display: flex;
			align-items: center;
			.iconNumber{
				font-size: 26rpx;
			}
		}
	}
}
</style>